From 3738fd4f72afffa33461822cc5bc28a389a97e5e Mon Sep 17 00:00:00 2001 From: Greg Sabino Mullane Date: Sun, 16 Jul 2006 15:20:24 +0000 Subject: [PATCH] Prevent full db open if class is being called only. Add lots of superuser magic to the initial connection. --- includes/DatabasePostgres.php | 104 ++++++++++++++++++++++++++++++---- 1 file changed, 94 insertions(+), 10 deletions(-) diff --git a/includes/DatabasePostgres.php b/includes/DatabasePostgres.php index 658828d74b..ad2ecb8473 100644 --- a/includes/DatabasePostgres.php +++ b/includes/DatabasePostgres.php @@ -52,6 +52,7 @@ class DatabasePostgres extends Database { throw new DBConnectionError( $this, "PostgreSQL functions missing, have you compiled PHP with the --with-pgsql option?\n" ); } + global $wgDBport; $this->close(); @@ -70,6 +71,10 @@ class DatabasePostgres extends Database { $hstring .= "port=$port "; } + if (!strlen($user)) { ## e.g. the class is being loaded + return; + } + error_reporting( E_ALL ); @$this->mConn = pg_connect("$hstring dbname=$dbName user=$user password=$password"); @@ -81,12 +86,79 @@ class DatabasePostgres extends Database { } $this->mOpened = true; - ## If this is the initial connection, setup the schema stuff - if (defined('MEDIAWIKI_INSTALL') and !defined('POSTGRES_SEARCHPATH')) { - global $wgDBmwschema, $wgDBts2schema, $wgDBname; + ## If this is the initial connection, setup the schema stuff and possibly create the user + if (defined('MEDIAWIKI_INSTALL')) { + global $wgDBname, $wgDBuser, $wgDBpass, $wgDBsuperuser, $wgDBmwschema, $wgDBts2schema; + print "OK\n"; + + ## Are we connecting as a superuser for the first time? + if ($wgDBsuperuser) { + $SQL = "SELECT 1 FROM pg_catalog.pg_user WHERE usename = " . $this->addQuotes($wgDBuser); + $rows = $this->numRows($this->doQuery($SQL)); + if ($rows) { + print "
  • User \"$wgDBuser\" already exists, skipping account creation.
  • "; + } + else { + ## Can we create users? + $SQL = "SELECT 1 FROM pg_catalog.pg_user WHERE usesuper IS TRUE AND ". + "usename = " . $this->addQuotes($wgDBsuperuser); + $rows = $this->numRows($this->doQuery($SQL)); + if (!$rows) { + print "
  • ERROR: the user \"$wgDBsuperuser\" cannot create other users. "; + print 'Please use a different Postgres user.
  • '; + dieout(''); + } + print "
  • Creating user $wgDBuser..."; + $safepass = $this->addQuotes($wgDBpass); + $SQL = "CREATE USER \"$wgDBuser\" NOCREATEDB PASSWORD $safepass"; + $this->doQuery($SQL); + print "OK
  • \n"; + } + ## User now exists, check out the database + $safename = $this->addQuotes($wgDBname); + $SQL = "SELECT 1 FROM pg_catalog.pg_database WHERE datname = $safename"; + $rows = $this->numRows($this->doQuery($SQL)); + if ($rows) { + print "
  • Database \"$wgDBname\" already exists, skipping database creation.
  • "; + } + else { + print "
  • Creating database $wgDBname..."; + $SQL = "CREATE DATABASE \"$wgDBname\" OWNER \"$wgDBuser\" "; + $this->doQuery($SQL); + print "OK
  • \n"; + ## Hopefully tsearch2 and plpgsql are in template1... + } + + ## Reconnect to check out tsearch2 rights for this user + print "
  • Connecting to \"$wgDBname\" as superuser \"$wgDBsuperuser\" to check rights..."; + @$this->mConn = pg_connect("$hstring dbname=$wgDBname user=$user password=$password"); + if ( $this->mConn == false ) { + print "FAILED TO CONNECT!
  • "; + dieout(""); + } + print "OK!"; + print "
  • Checking that tsearch2 is installed in the database \"$wgDBname\"..."; + if (! $this->tableExists("pg_ts_cfg", $wgDBts2schema)) { + print "FAILED. tsearch2 must be installed in the database \"$wgDBname\"."; + print "Please see 'http://www.devx.com/opensource/Article/21674/0/page/2'>this article"; + print " for instructions or ask on #postgresql on irc.freenode.net
  • \n"; + dieout(""); + } + print "OK\n"; + print "Ensuring that user \"$wgDBuser\" has select rights on the tsearch2 tables..."; + foreach (array('cfg','cfgmap','dict','parser') as $table) { + $SQL = "GRANT SELECT ON pg_ts_$table TO \"$wgDBuser\""; + $this->doQuery($SQL); + } + + $wgDBsuperuser = ''; + return true; ## Reconnect as regular user + } + + if (!defined('POSTGRES_SEARCHPATH')) { ## Do we have the basic tsearch2 table? - print "
  • Checking for tsearch2 ..."; + print "
  • Checking for tsearch2 in the schema \"$wgDBts2schema\"..."; if (! $this->tableExists("pg_ts_dict", $wgDBts2schema)) { print "FAILED. Make sure tsearch2 is installed. See this article"; @@ -95,6 +167,18 @@ class DatabasePostgres extends Database { } print "OK
  • \n"; + ## Does this user have the rights to the tsearch2 tables? + print "
  • Checking tsearch2 permissions..."; + $SQL = "SELECT 1 FROM $wgDBts2schema.pg_ts_cfg"; + error_reporting( 0 ); + $res = $this->doQuery($SQL); + error_reporting( E_ALL ); + if (!$res) { + print "FAILED. Make sure that the user \"$wgDBuser\" has SELECT access to the tsearch2 tables
  • \n"; + dieout(""); + } + print "OK"; + ## Do we have plpgsql installed? print "
  • Checking for plpgsql ..."; $SQL = "SELECT 1 FROM pg_catalog.pg_language WHERE lanname = 'plpgsql'"; @@ -112,20 +196,20 @@ class DatabasePostgres extends Database { print "
  • Creating schema $wgDBmwschema ..."; $result = $this->doQuery("CREATE SCHEMA $wgDBmwschema"); if (!$result) { - print "FAILED.
  • \n"; + print "FAILED.\n"; return false; } print "ok\n"; } else if ($result != $user) { - print "
  • Schema $wgDBmwschema exists but is not owned by $user. Not ideal.
  • \n"; + print "
  • Schema \"$wgDBmwschema\" exists but is not owned by \"$user\". Not ideal.
  • \n"; } else { - print "
  • Schema $wgDBmwschema exists and is owned by $user. Excellent.
  • \n"; + print "
  • Schema \"$wgDBmwschema\" exists and is owned by \"$user\". Excellent.
  • \n"; } ## Fix up the search paths if needed - print "
  • Setting the search path for user $user ..."; + print "
  • Setting the search path for user \"$user\" ..."; $path = "$wgDBmwschema"; if ($wgDBts2schema !== $wgDBmwschema) $path .= ", $wgDBts2schema"; @@ -134,7 +218,7 @@ class DatabasePostgres extends Database { $SQL = "ALTER USER $user SET search_path = $path"; $result = pg_query($this->mConn, $SQL); if (!$result) { - print "FAILED.
  • \n"; + print "FAILED.\n"; return false; } print "ok\n"; @@ -146,7 +230,7 @@ class DatabasePostgres extends Database { return false; } define( "POSTGRES_SEARCHPATH", $path ); - } + }} return $this->mConn; } -- 2.20.1